import 'dart:convert';
import 'package:clean_app/asset/storage.dart';
import 'package:dio/dio.dart';

bool isTestEnv = true;

String baseUrl = "https://";
String testBaseUrl = "http://124.223.16.213:8000";

class XHttp {
  netWorkRequest({
    Map<String, dynamic>? header, //请求头
    bool isRaw = true,
    required bool noTimeOut, //是否有超时
    required String url, //url地址
    required Map<String, dynamic> param, //参数
    required String method, //请求方式
  }) async {
    var dio = Dio();
    dio.options.baseUrl = isTestEnv ? testBaseUrl : baseUrl;
    dio.options.headers = header;
    dio.options.contentType =
        isRaw ? Headers.jsonContentType : Headers.formUrlEncodedContentType;
    dio.options.responseType = ResponseType.plain;
    dio.interceptors.add(QueuedInterceptorsWrapper(
      onError: (error, handler) async {
        if (error.response!.statusCode == 401) {
          setStorage(key: "token", value: "");
          handler.resolve(error.response!);
        } else if (error.response!.statusCode == 422) {
        } else if (error.response!.statusCode == 418) {
        } else {
          handler.resolve(error.response!);
        }
      },
    ));
    try {
      Response response = await dio.request(
        url,
        data: isRaw ? jsonEncode(param) : FormData.fromMap(param),
        options: Options(method: method),
      );
      if (response.statusCode == 401) {
        print("Token过期");
      }
      Map<String, dynamic> data = jsonDecode(response.toString());
      return data;
    } catch (err) {
      print("$err");
      return err;
    }
  }

  //带Token的请求
  Future putWithGlobalToken({
    String url = "",
    bool noTimeOut = false,
    bool slowLoading = false,
    bool isRaw = false,
    Map<String, dynamic>? param,
    int slowMilliseconds = 300,
  }) async {
    String token = await getStorage(key: "token", initData: "");
    var res = await netWorkRequest(
      url: url,
      method: "PUT",
      param: param ?? {},
      noTimeOut: noTimeOut,
      isRaw: isRaw,
      header: {"Authorization": "Bearer $token"},
    );
    return res;
  }

  //带Token的请求
  Future postWithGlobalToken({
    String url = "",
    bool noTimeOut = false,
    bool slowLoading = false,
    bool isRaw = false,
    Map<String, dynamic>? param,
    int slowMilliseconds = 300,
  }) async {
    String token = await getStorage(key: "token", initData: "");
    var res = await netWorkRequest(
      url: url,
      method: "POST",
      param: param ?? {},
      noTimeOut: noTimeOut,
      isRaw: isRaw,
      header: {"Authorization": "Bearer $token"},
    );
    return res;
  }

  //带Token的请求
  Future getWithGlobalToken({
    String url = "",
    bool noTimeOut = false,
    bool slowLoading = false,
    bool isRaw = false,
    Map<String, dynamic>? param,
    int slowMilliseconds = 300,
  }) async {
    String token = await getStorage(key: "token", initData: "");
    var res = await netWorkRequest(
      url: url,
      param: param ?? {},
      method: "GET",
      noTimeOut: noTimeOut,
      isRaw: isRaw,
      header: {"Authorization": "Bearer $token"},
    );
    return res;
  }

  //发起POST请求
  Future post({
    String url = "",
    Map<String, dynamic>? header,
    Map<String, dynamic>? param,
  }) async {
    return await netWorkRequest(
      url: url,
      method: "POST",
      header: header,
      param: param ?? {},
      noTimeOut: false,
    );
  }
}
